package cloud.seri.gateway.config

import io.github.jhipster.config.JHipsterProperties

import cloud.seri.gateway.gateway.ratelimiting.RateLimitingFilter
import cloud.seri.gateway.gateway.accesscontrol.AccessControlFilter
import cloud.seri.gateway.gateway.responserewriting.SwaggerBasePathRewritingFilter

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.cloud.netflix.zuul.filters.RouteLocator
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration
class GatewayConfiguration
{

    @Configuration
    class SwaggerBasePathRewritingConfiguration
    {
        @Bean
        fun swaggerBasePathRewritingFilter(): SwaggerBasePathRewritingFilter
        {
            return SwaggerBasePathRewritingFilter()
        }
    }

    @Configuration
    class AccessControlFilterConfiguration
    {
        @Bean
        fun accessControlFilter(routeLocator: RouteLocator, jHipsterProperties: JHipsterProperties): AccessControlFilter
        {
            return AccessControlFilter(routeLocator, jHipsterProperties)
        }
    }

    /**
     * Configures the Zuul filter that limits the number of API calls per user.
     *
     *
     * This uses Bucket4J to limit the API calls, see [io.github.jhipster.travis.gateway.ratelimiting.RateLimitingFilter].
     */
    @Configuration
    @ConditionalOnProperty("jhipster.gateway.rate-limiting.enabled")
    class RateLimitingConfiguration(private val jHipsterProperties: JHipsterProperties)
    {
        @Bean
        fun rateLimitingFilter(): RateLimitingFilter
        {
            return RateLimitingFilter(jHipsterProperties)
        }
    }
}
